
self.onmessage = function (e) {
  const { command, data } = e.data;
  switch (command) {
    case 'parsingData': {
      // 计算分数表现
      function calcIntegralSettingData (score) {
        score = parseInt(score)
        for (let i = 0; i < data.integralSettingData.iconSettingList.length; i++) {
          const v = data.integralSettingData.iconSettingList[i];
          if (v.min <= score && v.max >= score) {
            return v
          }
        }
      }
      // 已经连接的笔
      const matchingPenMapData: any = {}
      data.matchingPenData.forEach(item => {
        matchingPenMapData[item.studentInfo.id] = {
          studentId: item.studentInfo.id,
          studentInfo: item.studentInfo,
          listData: []
        };
      })
      // 排序后的结果集合
      let sortData: any[] = []
      // 查字排序后的结果集合
      let sortDescData: any[] = []
      // 排序后的学生集合
      let sortStudentData: any[] = []
      // 结束课程要提交的数据
      let submitData: any[] = []
      // 全部字的集合
      let listData: any[] = []
      // 字的合计 总数，完成数，一般，良好，优秀
      let wordNum = {
        total: 0,
        complete: 0,
        scope1: 0,
        scope2: 0,
        scope3: 0,
      };
      listData = data.listData.map(item => {
        const jsonData = JSON.parse(item.jsonData)
        const bookData = JSON.parse(item.bookData)
        // 六个维度评分
        const scoreData = [
          jsonData.scope.orderCalculationScore,
          jsonData.scope.structureCalculationScore,
          jsonData.scope.speedCalculationScore,
          jsonData.scope.strengthCalculationScore,
          jsonData.scope.strokeCalculationScore,
          jsonData.scope.countCalculationScore,
        ];
        // 平均分
        const averageScore = scoreData.reduce((prve, curr) => {
          return prve + curr;
        }, 0) / 6;
        // 评分结果
        const integralResult = calcIntegralSettingData(averageScore)
        wordNum.complete++;
        if (integralResult.scope === 1) {
          wordNum.scope1++;
        } else if (integralResult.scope === 2) {
          wordNum.scope2++;
        } else if (integralResult.scope === 3) {
          wordNum.scope3++;
        }
        return {
          ...item,
          jsonData: jsonData,
          bookData: bookData,
          writeInfo: {
            image: `${data.courseId}-${item.mac}-${item.studentId}-${Number(item.regionId)}-${Number(item.regionIndex)}.webp`,
            label: '已完成',
            icon: integralResult.icon
          },
          radarData: scoreData,
          orderCalculationScore: jsonData.scope.orderCalculationScore,
          structureCalculationScore: jsonData.scope.structureCalculationScore,
          speedCalculationScore: jsonData.scope.speedCalculationScore,
          strengthCalculationScore: jsonData.scope.strengthCalculationScore,
          strokeCalculationScore: jsonData.scope.strokeCalculationScore,
          countCalculationScore: jsonData.scope.countCalculationScore,
          averageScore: averageScore,
          // 模版字 ID
          wordTemplateId: jsonData.modelRegion.region.templateId,
          // 模版字 图片
          wordTemplateUrl: jsonData.modelRegion.fontPic,
          // 所在区域最大Index
          maxIndex: jsonData.maxIndex,
          // 评分结果 档位
          integralResultScope: integralResult.scope,
        }
      })
      for (const item of listData) {
        if (matchingPenMapData[item.studentId]) {
          matchingPenMapData[item.studentId].listData.push(item);
        }
      }
      for (const key in matchingPenMapData) {
        const wordData = {}
        const studentWordNum = {
          total: 0,
          complete: 0,
          scope1: 0,
          scope2: 0,
          scope3: 0,
        }
        const radarData = {
          orderCalculationScore: 0,
          structureCalculationScore: 0,
          speedCalculationScore: 0,
          strengthCalculationScore: 0,
          strokeCalculationScore: 0,
          countCalculationScore: 0,
        }
        for (const item of matchingPenMapData[key].listData) {
          studentWordNum.complete++;
          radarData.orderCalculationScore += item.orderCalculationScore;
          radarData.structureCalculationScore += item.structureCalculationScore;
          radarData.speedCalculationScore += item.speedCalculationScore;
          radarData.strengthCalculationScore += item.strengthCalculationScore;
          radarData.strokeCalculationScore += item.strokeCalculationScore;
          radarData.countCalculationScore += item.countCalculationScore;
          if (wordData[item.wordTemplateId]) {
            wordData[item.wordTemplateId].data.push(item);
          } else {
            wordNum.total = wordNum.total + item.maxIndex;
            studentWordNum.total = studentWordNum.total + item.maxIndex;
            wordData[item.wordTemplateId] = {
              wordTemplateId: item.wordTemplateId,
              wordTemplateUrl: item.wordTemplateUrl,
              maxIndex: item.maxIndex,
              data: [item]
            };
          }
        }
        matchingPenMapData[key].averageScore = matchingPenMapData[key].listData.length === 0 ? 0 : Number((matchingPenMapData[key].listData.reduce((prve, curr) => {
          return prve + curr.averageScore;
        }, 0) / matchingPenMapData[key].listData.length).toFixed(2));
        const studentIntegralResult = calcIntegralSettingData(matchingPenMapData[key].averageScore)
        matchingPenMapData[key].scoreScope = studentIntegralResult.scope
        matchingPenMapData[key].scoreIcon = studentIntegralResult.icon

        matchingPenMapData[key].radarData = [
          radarData.orderCalculationScore / matchingPenMapData[key].listData.length,
          radarData.structureCalculationScore / matchingPenMapData[key].listData.length,
          radarData.speedCalculationScore / matchingPenMapData[key].listData.length,
          radarData.strengthCalculationScore / matchingPenMapData[key].listData.length,
          radarData.strokeCalculationScore / matchingPenMapData[key].listData.length,
          radarData.countCalculationScore / matchingPenMapData[key].listData.length,
        ];
        matchingPenMapData[key].wordData = wordData
        matchingPenMapData[key].wordNum = studentWordNum
        // 分数从高到低
        matchingPenMapData[key].listData.sort((a, b) => {
          return a.averageScore < b.averageScore ? 1 : -1;
        })
        // 取每个学生排名前2的字
        matchingPenMapData[key].listData.forEach((item, i) => {
          if (i === 0 || i === 1) {
            sortData.push(item)
          }
        })
        // 每个学生排名前2的字 再排一次序
        sortData.sort((a, b) => {
          return a.averageScore < b.averageScore ? 1 : -1;
        })

        // 分数从低到高
        matchingPenMapData[key].listData.sort((a, b) => {
          return a.averageScore > b.averageScore ? 1 : -1;
        })
        // 取每个学生排名后2的字
        matchingPenMapData[key].listData.forEach((item, i) => {
          if (i === 0 || i === 1) {
            sortDescData.push(item)
          }
        })
        // 每个学生排名后2的字 再排一次序
        sortDescData.sort((a, b) => {
          return a.averageScore > b.averageScore ? 1 : -1;
        })

        sortStudentData.push({
          studentInfo: matchingPenMapData[key].studentInfo,
          wordNum: studentWordNum,
          averageScore: matchingPenMapData[key].averageScore
        })
        submitData.push({
          integral: matchingPenMapData[key].averageScore,
          scopes: matchingPenMapData[key].listData.map(item => {
            return {
              "advantageList": item.jsonData.scope.advantageList,
              "countScore": item.jsonData.scope.countCalculationScore,
              "disadvantageList": item.jsonData.scope.disadvantageList,
              "handwritingSummary": item.jsonData.handwritingSummary,
              "handwritingUrl": item.jsonData.url,
              "orderScore": item.jsonData.scope.orderCalculationScore,
              "practiceTime": item.practiceTime,
              "regionId": item.regionId,
              "regionIndex": item.regionIndex,
              "score": Number(matchingPenMapData[key].listData.reduce((prve, curr) => {
                return prve + curr.averageScore;
              }, 0)),
              "speedScore": item.jsonData.scope.speedCalculationScore,
              "strengthScore": item.jsonData.scope.strengthCalculationScore,
              "strokeScore": item.jsonData.scope.strokeCalculationScore,
              "structureScore": item.jsonData.scope.structureCalculationScore,
              "templateHandwritingUrl": item.jsonData.modelRegion.url,
              "templateId": item.jsonData.modelRegion.region.templateId,
              "textBookId": item.bookData.textBookId,
              "textBookPageId": item.bookData.textBookPageId,
            }
          }),
          studentId: matchingPenMapData[key].studentInfo.id,
          totalCount: studentWordNum.total
        })
      }
      sortStudentData.sort((a, b) => {
        return a.wordNum.scope3 < b.scope3 ? 1 : -1;
      })
      self.postMessage({
        command: 'returnData',
        data: {
          wordNum: wordNum,
          resultMap: matchingPenMapData,
          sortData: sortData,
          sortDescData: sortDescData,
          sortStudentData: sortStudentData,
          submitData: submitData,
          courseAverageScore: Number(sortStudentData.reduce((prve, curr) => {
            return prve + curr.averageScore;
          }, 0)).toFixed(2),
          realTimePeopleNum: data.matchingPenData.length
        },
      });

    }
  }

  // self.postMessage({
  //   command: 'draw',
  //   data: {
  //     // canvas: canvas
  //     text: '测试信息收发' + Date.now(),
  //     image: context.getImageData(0, 0, 280, 280),
  //
  //     data: k20,
  //   },
  // });
};
